﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Windows.Input;
using VASJ.Common;
using VASJ.Model;

namespace VASJ.Presentation
{
    public partial class FrmConfiguration : Form
    {
        public FrmConfiguration()
        {
            InitializeComponent();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            // Shortcut key
            ShortcutKey shortcutKey = ConfigManager.ConfigurationInstant.ShortcutKeys.FirstOrDefault(s => s.Function == Functions.OpenConfiguration);

            if (shortcutKey == null)
            {
                shortcutKey = new ShortcutKey();
                shortcutKey.Function = Functions.OpenConfiguration;
                ConfigManager.ConfigurationInstant.ShortcutKeys.Add(shortcutKey);
            }

            shortcutKey.Keys = new List<string>();

            if (this.chkCtrl.Checked)
            {
                shortcutKey.Keys.Add(Key.LeftCtrl.ToString());
                shortcutKey.Keys.Add(Key.RightCtrl.ToString());
            }
            if (this.chkAlt.Checked)
            {
                shortcutKey.Keys.Add(Key.LeftAlt.ToString());
                shortcutKey.Keys.Add(Key.RightAlt.ToString());
            }
            if (this.chkShift.Checked)
            {
                shortcutKey.Keys.Add(Key.LeftShift.ToString());
                shortcutKey.Keys.Add(Key.RightShift.ToString());
            }

            string sKey = this.txtKey.Text;
            if (!string.IsNullOrEmpty(sKey) && (sKey == "0" || sKey == "1" || sKey == "2" || sKey == "3" || sKey == "4" || sKey == "5" || sKey == "6" || sKey == "7" || sKey == "8" || sKey == "9"))
            {
                shortcutKey.Keys.Add("D" + sKey);
            }
            else
            {
                shortcutKey.Keys.Add(this.txtKey.Text);
            }

            ConfigManager.Save();
        }

        private void FrmConfiguration_Load(object sender, EventArgs e)
        {
            InitializeForm();
        }

        private void InitializeForm()
        {
            //Load default shortcut key
            foreach (ShortcutKey shortcutKey in ConfigManager.ConfigurationInstant.ShortcutKeys)
            {
                var keys = shortcutKey.Keys;
                chkCtrl.Checked = false;
                chkAlt.Checked = false;
                chkShift.Checked = false;
                for (int i = 0; i < keys.Count; i++)
                {
                    KeyConverter k = new KeyConverter();
                    Key myKey = (Key)k.ConvertFromString(keys[i]);

                    if (myKey == Key.LeftCtrl || myKey == Key.RightCtrl)
                    {
                        chkCtrl.Checked = true;
                        continue;
                    }
                    else if (myKey == Key.LeftAlt || myKey == Key.RightAlt)
                    {
                        chkAlt.Checked = true;
                        continue;
                    }
                    else if (myKey == Key.LeftShift || myKey == Key.RightShift)
                    {
                        chkShift.Checked = true;
                        continue;
                    }
                    else
                    {
                        string sKey = myKey.ToString();
                        if (sKey == "D0" || sKey == "D1" || sKey == "D2" || sKey == "D3" || sKey == "D4" || sKey == "D5" || sKey == "D6" || sKey == "D7" || sKey == "D8" || sKey == "D9")
                        {
                            txtKey.Text = sKey.Remove(0, 1);
                        }
                        else
                        {
                            txtKey.Text = myKey.ToString();
                        }
                    }
                }
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {

        }
    }
}